package com.yang.eduauth.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yang.commonutils.R;

import com.yang.eduauth.entity.Role;


import com.yang.eduauth.service.PermissionService;
import com.yang.eduauth.service.RolePermissionService;
import com.yang.eduauth.service.RoleService;
import com.yang.eduauth.vo.RoleQuery;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yds
 * @since 2020-12-16
 */
@RestController
@RequestMapping("/eduauth/role")
@Api(description = "角色管理")
@CrossOrigin
public class RoleController {

    @Autowired
    RoleService roleService;
    @Autowired
    PermissionService permissionService;
    @Autowired
    RolePermissionService rolePermissionService;



    /**
     * 获取所有讲师
     * @return
     */
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public R findAllRole(){
        List<Role> list = roleService.list(null);
        return R.ok().data("items",list);
    }

    /**
     * 逻辑删除讲师
     * @param id
     * @return
     */
    @ApiOperation("逻辑删除角色")
    @DeleteMapping("/{id}")
    public R removeRole(@ApiParam(name="id",value="角色id",required = true)
                           @PathVariable String id){
        boolean flag = roleService.removeById(id);
        return flag?R.ok():R.error();

    }

    @ApiOperation("分页查询用户")
    @GetMapping("/pageRole/{current}/{limit}")
    public R pageListTeacher(@ApiParam(name="current",value="当前页",required = true)
                             @PathVariable long current,
                             @ApiParam(name="limit",value="每页数据",required = true)
                             @PathVariable long limit){

        //创建page对象
        Page<Role> pageUser=new Page<Role>(current,limit);
        roleService.page(pageUser, null);
        long total = pageUser.getTotal();
        List<Role> records = pageUser.getRecords();
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);

    }

    /***
     * 条件分页查询
     * @param current
     * @param limit
     * @param roleQuery
     * @RequestBody 将post请求的json分装到注解对应的实体里
     * @return
     */
    @ApiOperation("角色条件查询分页")
    @PostMapping("/pageRoleCondition/{current}/{limit}")
    public R pageRoleCondition(@ApiParam(name="current",value="当前页",required = true)
                                  @PathVariable long current,
                                  @ApiParam(name="limit",value="每页数据",required = true)
                                  @PathVariable long limit,
                                  @RequestBody(required = false) RoleQuery roleQuery){
        //创建page对象
        Page<Role> pageRole=new Page<Role>(current,limit);
        QueryWrapper<Role> wrapper=new QueryWrapper<Role>();
        String name = roleQuery.getName();
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        IPage<Role> page = roleService.page(pageRole, wrapper);
        long total = pageRole.getTotal();
        List<Role> records = pageRole.getRecords();
        Map map=new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return R.ok().data(map);

    }
    @ApiOperation("增加角色接口")
    @PostMapping("/addUser")
    public R addRole(@RequestBody Role role){
        boolean save = roleService.save(role);
        return save?R.ok():R.error();
    }
    @ApiOperation("根据id获取角色信息")
    @GetMapping("/getUser/{id}")
    public R getRole(@PathVariable String id){
        Role role = roleService.getById(id);
        return R.ok().data("item",role);
    }
    @ApiOperation("修改角色信息")
    @PostMapping("/updateUser")
    public R updateRole(@RequestBody Role role){
        boolean b = roleService.updateById(role);
        return b?R.ok():R.error();
    }





}

